package com.project.hf.utils;

import javax.sql.rowset.JdbcRowSet;
import java.sql.*;
import java.util.*;
import java.util.Date;

import org.omg.CORBA.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;


public class MysqlConn {



    private String JDBC_DRIVER;

    private String DB_URL;

    // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
    //static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    //static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC";


    // 数据库的用户名与密码，需要根据自己的设置
    private String USER;
    private String PASS;

    public static void main(String[] args) {


    }

    public Connection GetMysqlHFConn() {

        Map<String,String> dbcfg=ReadConfig.getConfigUtil().getDBConfig();
        MysqlHF T1 = new MysqlHF(dbcfg.get("Mysql_JDBC_DRIVER"), dbcfg.get("Mysql_DB_URL"),
                                dbcfg.get("Mysql_USER"), dbcfg.get("Mysql_PASS"));
        Connection m =T1.run();
        return m;
    }

}


class MysqlHF  {

    private String drive;
    private String dburl;
    private String user;
    private String pass;

    MysqlHF(String drive1, String dburl1, String user1, String pass1) {
        drive = drive1;
        dburl = dburl1;
        user = user1;
        pass = pass1;
    }

    public Connection run() {

        Connection conn = null;
//        Statement stmt = null;
//        Map<String, String> map = new HashMap<String, String>();
        try {
            // 注册 JDBC 驱动
            Class.forName(drive);

            // 打开链接
//            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(dburl,user,pass);

            // 执行查询
//            System.out.println(" 实例化Statement对象...");
//            stmt = conn.createStatement();
//            String sql;
//            sql = "SELECT `type`,label FROM crud_dict";
//            ResultSet rs = stmt.executeQuery(sql);



//            stmt.close();
//            conn.close();
            return conn;
        } catch (SQLException se) {
            // 处理 JDBC 错误
            System.out.print("sql\n");
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            System.out.print("class \n");
            e.printStackTrace();
        }
        return conn;

    }

}
